rm(list = ls())

library(data.table)
library(haven)


# Asian and Latino PID from Nationscape Data around 2020 election

files <- lapply(Sys.glob("./replication_hasz/Nationscape-Weekly-Materials-DTA-2021Dec/phase_1_v20210301/ns*/ns*.dta"), read_dta)
phase1 <- rbindlist(files, use.names=TRUE, fill=TRUE)

files <- lapply(Sys.glob("./replication_hasz/data/Nationscape-Weekly-Materials-DTA-2021Dec/phase_2_v20210301/ns*/ns*.dta"), read_dta)
phase2 <- rbindlist(files, use.names=TRUE, fill=TRUE)

files <- lapply(Sys.glob("./replication_hasz/data/Nationscape-Weekly-Materials-DTA-2021Dec/phase_3_v20210301/ns*/ns*.dta"), read_dta)
phase3 <- rbindlist(files, use.names=TRUE, fill=TRUE)

files <- lapply(Sys.glob("./replication_hasz/data/Nationscape-Weekly-Materials-DTA-2021Dec/phase_parallel_v20210301/ns*/ns*.dta"), read_dta)
phase4 <- rbindlist(files, use.names=TRUE, fill=TRUE)

ns <- rbind(phase1, phase2, phase3, phase4, use.names=TRUE, fill=TRUE)
ns[, democrat:=ifelse(pid3==1,1,0)]
ns[, republican:=ifelse(pid3==2,1,0)]
ns[, independent:=ifelse(pid3==3,1,0)]
ns[, `something else`:=ifelse(pid3==4,1,0)]

pid_latino <- ns[((hispanic > 1 & hispanic <= 15) & state!=''), .(democrat=mean(democrat, na.rm=TRUE),
                                                                  republican=mean(republican, na.rm=TRUE),
                                                                  independent=mean(independent, na.rm=TRUE),
                                                                  `something else`=mean(`something else`, na.rm=TRUE),
                                                                  `num obs`=.N), by=state]
pid_latino <- pid_latino[order(state)]

pid_asian <- ns[((race_ethnicity>=4 & race_ethnicity<=10) & state!=''), .(democrat=mean(democrat, na.rm=TRUE),
                                                                          republican=mean(republican, na.rm=TRUE),
                                                                          independent=mean(independent, na.rm=TRUE),
                                                                          `something else`=mean(`something else`, na.rm=TRUE),
                                                                          `num obs`=.N), by=state]
pid_asian <- pid_asian[order(state)]

latino_asian_table <- merge(pid_asian, pid_latino, by='state', all = TRUE)
colnames(latino_asian_table) <- c('State','Democrat','Republican','Independent','Something else','Num. obs.','Democrat','Republican','Independent','Something else','Num. obs.')

print(xtable::xtable(latino_asian_table,
                     caption="Asian and Latino Party Identification",
                     digits=c(0,0,2,2,2,2,0,2,2,2,2,0),
                     caption.placement='top',
                     align='lccccccccccc'),
      file = './replication_hasz/output/tables/tabE5.tex',
      include.rownames=FALSE,
      caption.placement = "top")

